package com.boco.pciv.controller.project;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.controller.company.CompanyController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.entity.sale.ManageInfo;
import com.boco.pciv.entity.sale.ManagePayInfo;
import com.boco.pciv.service.sale.IManageCheckService;
import com.boco.pciv.service.sale.IManagePayCheckService;
import com.boco.pciv.service.sale.IManagePayService;
import com.boco.pciv.service.sale.IManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sunrise on 2020/7/16.
 */
@RestController
@RequestMapping(value="/manage/")
public class ManageController extends PcivController {
    Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @Resource
    private IManageService iManageService;
    @Resource
    private IManageCheckService iManageCheckService;
    @Resource
    private IManagePayCheckService iManagePayCheckService;
    @Resource
    private IManagePayService iManagePayService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public PageResultBase list(String saleNo,  LayUIPager pager) {
        SearchCriteria param = new SearchCriteria();
        param.setPager(pager);
        if (StrUtil.isNotBlank(saleNo)) {
            param.putCriteria("saleNo", saleNo);
        }
        PageResultBase result = iManageCheckService.getPage(param);
        return result;
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public BizResponse info(String  manageNo,String op) {
        CriteriaBase cond = new CriteriaBase();
        cond.putCriteria("manageNo", manageNo);
        if (StrUtil.isNotBlank(op)) {
            ManageInfo info = iManageService.getOne(cond);
            return BizResponseUtil.single(info);
        } else {
            ManageInfo info = iManageCheckService.getOne(cond);
            return BizResponseUtil.single(info);
        }
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    public BizResponse add(ManageInfo info) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("add data:{}", JSONUtil.toJsonStr(info));
            }
            if (info == null) {
                return BizResponseUtil.fail("新增信息不能为空");
            }
            info.setManageNo(Integer.parseInt(RandomUtil.randomNumbers(8)));
            info.setPayAmount(new BigDecimal(0));
            info.setBalanceAmount(info.getAmount());
            iManageCheckService.add(info);
            iManageService.add(info);
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (Exception e) {
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR);
        }
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    public BizResponse update(ManageInfo info) {
        try {
            if (info == null) {
                return BizResponseUtil.fail("修改信息不能为空");
            }
            SearchCriteria cond=new SearchCriteria();
            cond.setIsPage(false);
            cond.putCriteria("manageNo",info.getManageNo());
            List<ManagePayInfo> list = iManagePayCheckService.getList(cond);
            BigDecimal payAmount = new BigDecimal(0);
            for (ManagePayInfo l : list) {
                payAmount = payAmount.add(l.getPayAmount());
            }
            if (info.getAmount().compareTo(payAmount) == -1) {
                return BizResponseUtil.fail("监管金额 不能比已填报的 付款金额小 ，填写有误");
            }
            info.setPayAmount(payAmount);
            info.setBalanceAmount(info.getAmount().subtract(payAmount));
            iManageCheckService.edit(info);
            iManageService.edit(info);
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (Exception e) {
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR);
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public BizResponse delete(String  manageNo) {
        try {
            CriteriaBase cond = new CriteriaBase();
            cond.putCriteria("manageNo", manageNo);
            iManageCheckService.delete(cond);
            iManagePayCheckService.delete(cond);

            iManageService.delete(cond);
            iManagePayService.delete(cond);

            return BizResponseUtil.success(MSG_ExeOK);
        } catch (Exception e) {
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR);
        }
    }


}
